class ConCaptureObjective extends ConObjective;

var(ConProperties) repnotify StaticMeshComponent Team0Mesh, Team1Mesh;
var repnotify int ConCaptureStage;

replication
{
	if (Role == ROLE_Authority)
		ConCaptureStage;
}

simulated event PostBeginPlay()
{
	Reset();
	Super.PostBeginPlay();
}

function bool Shootable()
{
	return false;
}

simulated function Reset()
{
	ConCaptureStage = 0;
	Team0Mesh.SetHidden(False);
	Team1Mesh.SetHidden(True);
	
	Super.Reset();
}

simulated function ConCapture()
{
	ConCaptureStage = 1;
	Health = 0;
	Team0Mesh.SetHidden(True);
	Team1Mesh.SetHidden(False);
	ConqueredCastles(WorldInfo.Game).ObjectiveComplete(self);
	
	bConComplete = true;
}

simulated function CheckHealth()
{
	if (bConComplete == false)
	{
		if(health <= 0) ConCapture();
	}
}

simulated event TakeDamage(int Damage, Controller InstigatedBy, vector HitLocation, vector Momentum, class<DamageType> DamageType, optional TraceHitInfo HitInfo, optional Actor DamageCauser)
{
	
	// if (Damage <= 0 || WorldInfo.Game.bGameEnded || UTGame(WorldInfo.Game).ResetCountdown > 0)
	// 	return;
		
    // if(health > 0) Health -= Damage;
    // if(health <= 0) ConCapture();

    bForceNetUpdate = true;
}

simulated event PostRenderFor(PlayerController PC, Canvas Canvas, vector CameraPosition, vector CameraDir)
{
        local float XL, HealthX;
        local vector ScreenLoc;

        ScreenLoc = Canvas.Project(Location) ;

    // make sure not clipped out
        if (screenLoc.X < 0 ||
                screenLoc.X >= Canvas.ClipX ||
                screenLoc.Y < 0 ||
                screenLoc.Y >= Canvas.ClipY)
        {
                return;
        }

       healthX = health*100/BaseHealth;
       XL = 100 ;
           class'UTHUD'.static.DrawHealth(ScreenLoc.X-0.5*XL, ScreenLoc.Y - 50, HealthX, XL, 20, Canvas);
}

// Kyle: Begin AI Controls

function bool TellBotHowToDisable(UTBot B)
{
	return TellBotHowToInteract(B);
}

function bool TellBotHowToHeal(UTBot B)
{
	return TellBotHowToInteract(B);
}

function bool TellBotHowToInteract(UTBot B)
{
	`log("Con: ConCaptureObjective - TellBotHowToInteract");

	if (NearObjective(B.Pawn))
	{
		return false;
	} else {
		return B.Squad.FindPathToObjective(B, self);
	}

	if ( !B.Pawn.bStationary && B.Pawn.TooCloseToAttack(self) )
	{
		B.GoalString = "Back off from objective";
		B.RouteGoal = B.FindRandomDest();
		B.MoveTarget = B.RouteCache[0];
		B.SetAttractionState();
		return true;
	}
	
	MarkShootSpotsFor(B.Pawn);
}

function bool NearObjective(Pawn P)
{
	return (VSize(Location - P.Location) < BaseRadius && P.LineOfSightTo(self));
}

// Kyle: End AI Controls

defaultproperties
{
   RemoteRole=ROLE_SimulatedProxy
   ConCaptureStage=0
   BaseHealth=500
   CollisionType=COLLIDE_BlockAll
   BlockRigidBody=true
   bStatic=False
   bNoDelete=True
   bDestinationOnly=false
   bAlwaysRelevant=true

   NetUpdateFrequency=1
   Name="ConCaptureObjective"
   ConObjectiveType=CO_Hold
}